using Aurora.TaskScheduler.Abstractions.Message;
using Microsoft.Extensions.Options;
using SqlSugar;

namespace Aurora.TaskScheduler.Storage.MySql;

/// <summary>
/// sql sugar context
/// </summary>
public class MySqlSugarContext
{
    public readonly SqlSugarScope Scope;
    private readonly IOptions<MySqlOptions> _options;
    public MySqlSugarContext( IOptions<MySqlOptions> options)
    {
        _options = options;
        SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = _options.Value.ConnectionString,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (s, p) =>
                    {
                        p.IfTable<AppTask>()
                            .UpdateProperty(it => it.Id, it =>
                            {
                                it.IsPrimarykey = true;
                                it.DataType = "varchar(50)";
                                it.ColumnDescription = "任务编号";
                            })
                            .UpdateProperty(it => it.TaskName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "任务名";
                            })
                            .UpdateProperty(it => it.GroupName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "分组名";
                            })
                            .UpdateProperty(it => it.Interval, it =>
                            {
                                it.DataType = "varchar(100)";
                                it.ColumnDescription = "间隔时间";
                            })
                            .UpdateProperty(it => it.ApiUrl, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "调用的API地址";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.Describe, it =>
                            {
                                it.DataType = "text";
                                it.ColumnDescription = "任务描述";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.LastRunTime, it =>
                            {
                                it.DataType = "datetime";
                                it.ColumnDescription = "最近一次运行时间";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.Status, it =>
                            {
                                it.DataType = "int";
                                it.ColumnDescription = "运行状态";
                            })
                            .UpdateProperty(it => it.TaskType, it =>
                            {
                                it.DataType = "int";
                                it.ColumnDescription = "任务类型(1.DLL类型,2.API类型)";
                            })
                            .UpdateProperty(it => it.ApiRequestType, it =>
                            {
                                it.DataType = "varchar(100)";
                                it.ColumnDescription = "API访问类型(API类型) get post ";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.ApiAuthKey, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "授权名(API类型)";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.ApiAuthValue, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "授权值(API类型)";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.ApiParameter, it =>
                            {
                                it.DataType = "varchar(5000)";
                                it.ColumnDescription = "API参数";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.DllClassName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "DLL类型名";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.DllActionName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "Dll方法名";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.ArgumentTypeName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "参数类型名称";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.CreatedTime, it =>
                            {
                                it.DataType = "varchar(50)";
                                it.ColumnDescription = "创建时间";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.LastModifiedTime, it =>
                            {
                                it.DataType = "varchar(50)";
                                it.ColumnDescription = "最后修改时间";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.IntervalDescription, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "间隔时间的描述";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.TaskExecuteType, it =>
                            {
                                it.DataType = "varchar(10)";
                                it.ColumnDescription = "任务执行类型 CRON 代表周期性  ONE 代表只执行一次  执行一次之后 就自动删除";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(x => x.StatusName, it =>
                            {
                                it.IsIgnore = true;
                            })
                            .UpdateProperty(x => x.NextRunTime, it =>
                            {
                                it.IsIgnore = true;
                            })
                            .UpdateProperty(x => x.TaskTypeName, it =>
                            {
                                it.IsIgnore = true;
                            });
                        p.IfTable<AppTaskLog>()
                            .UpdateProperty(it => it.Id, it =>
                            {
                                it.IsPrimarykey = true;
                                it.DataType = "varchar(50)";
                                it.ColumnDescription = "主键编号";
                            })
                            .UpdateProperty(it => it.TaskId, it =>
                            {
                                it.DataType = "varchar(50)";
                                it.ColumnDescription = "任务编号";
                            })
                            .UpdateProperty(it => it.TaskName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "任务名";
                            })
                            .UpdateProperty(it => it.GroupName, it =>
                            {
                                it.DataType = "varchar(500)";
                                it.ColumnDescription = "分组名";
                            })
                            .UpdateProperty(it => it.BeginDate, it =>
                            {
                                it.DataType = "datetime";
                                it.ColumnDescription = "任务开始时间";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.EndDate, it =>
                            {
                                it.DataType = "datetime";
                                it.ColumnDescription = "任务结束时间";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.Msg, it =>
                            {
                                it.DataType = "text";
                                it.ColumnDescription = "任务执行结果";
                                it.IsNullable = true;
                            })
                            .UpdateProperty(it => it.ElapsedMilliseconds, it =>
                            {
                                it.DataType = "bigint";
                                it.ColumnDescription = "耗时（毫秒）";
                            })
                            .UpdateProperty(it => it.Success, it =>
                            {
                                it.DataType = "bit";
                                it.ColumnDescription = "是否成功";
                            })  .UpdateProperty(it => it.Exception, it =>
                            {
                                it.DataType = "text";
                                it.IsNullable = true;
                                it.ColumnDescription = "异常信息";
                            })  .UpdateProperty(it => it.CreateTime, it =>
                            {
                                it.DataType = "datetime";
                                it.ColumnDescription = "创建时间";
                                it.IsNullable = true;
                            });
                    }
                }
            },
            db =>
            {
                if (options.Value.OpenLogger)
                {
                    db.Aop.OnLogExecuting = (sql, param) =>
                    {
                        //打印sql 日志
                        Console.WriteLine("[TaskScheduler.Mysql执行]" + sql);
                    };
                }
            }
        );
        Scope = sqlSugar;
        
        Scope.CodeFirst.As<AppTask>($"{(string.IsNullOrEmpty(_options.Value.DefaultTablePrefix)?"":(_options.Value.DefaultTablePrefix+"_"))}AppTask").InitTables<AppTask>();
        Scope.CodeFirst.As<AppTaskLog>($"{(string.IsNullOrEmpty(_options.Value.DefaultTablePrefix)?"":(_options.Value.DefaultTablePrefix+"_"))}AppTaskLog").InitTables<AppTaskLog>();
        
    }
}